package cn.snowsoft.iot.module.cps.controller.admin.cpsRule;

import cn.snowsoft.iot.framework.common.pojo.CommonResult;
import cn.snowsoft.iot.module.cps.controller.admin.cpsRule.vo.CpsActionOutputVO;
import cn.snowsoft.iot.module.cps.controller.admin.cpsRule.vo.CpsDataEntryVO;
import cn.snowsoft.iot.module.cps.controller.admin.cpsRule.vo.CpsRulePageVO;
import cn.snowsoft.iot.module.cps.controller.admin.cpsRule.vo.CpsRuleSaveVO;
import cn.snowsoft.iot.module.cps.controller.admin.cpsRule.vo.DeviceProductVO;
import cn.snowsoft.iot.module.cps.dal.dataobject.cpsRule.CpsRule;
import cn.snowsoft.iot.module.cps.service.cpsRule.CpsRuleService;
import cn.snowsoft.iot.module.cps.service.product.ProductService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException;
import java.util.List;

@RestController
@AllArgsConstructor
@RequestMapping("/cps/cpsRule")
@Tag(name = "cps规则 - 规则管理接口")
public class CpsRuleController {
    private final CpsRuleService cpsRuleService;

    @PostMapping("/submit")
    @ApiOperationSupport(order = 3)
    @Operation(summary = "新增或修改")
    public CommonResult<CpsRuleSaveVO> submit(@RequestBody CpsRuleSaveVO cpsRuleSaveVO) throws IOException {
        cpsRuleService.submit(cpsRuleSaveVO);
        return CommonResult.success(cpsRuleSaveVO);
    }
    @PostMapping("/saveRuleInformation")
    @ApiOperationSupport(order = 3)
    public CommonResult saveRuleInformation(@RequestBody DeviceProductVO deviceProductVO) {
        cpsRuleService.saveOrUpdateRule(deviceProductVO);
        return CommonResult.success(true);
    }

    @GetMapping("/detail")
    @ApiOperationSupport(order = 1)
    public CommonResult<CpsRuleSaveVO> detail(Long id) throws JsonProcessingException {
        CpsRule cpsRule1 = cpsRuleService.lambdaQuery().eq(CpsRule::getId, id).one();
        CpsRuleSaveVO equipmentVO = new CpsRuleSaveVO();
        BeanUtils.copyProperties(cpsRule1, equipmentVO);
        ObjectMapper objectMapper = new ObjectMapper();
        if (ObjectUtils.isNotEmpty(equipmentVO.getRuleInput())) {
            equipmentVO.setCpsDataEntries(objectMapper.readValue(equipmentVO.getRuleInput(), new TypeReference<List<CpsDataEntryVO>>() {
            }));
        }
        if (ObjectUtils.isNotEmpty(equipmentVO.getAction())) {
            equipmentVO.setCpsActionOutputs(objectMapper.readValue(equipmentVO.getAction(), new TypeReference<List<CpsActionOutputVO>>() {
            }));
        }
        return CommonResult.success(equipmentVO);
    }

    @GetMapping("/page")
    @ApiOperationSupport(order = 2)
    public CommonResult<IPage<CpsRulePageVO>> page(CpsRulePageVO cpsRulePageVO) {
        IPage<CpsRulePageVO> pages = cpsRuleService.selectcpsRulePage(cpsRulePageVO);
        return CommonResult.success(pages);
    }

}